use "C:\Users\ptucker\Dropbox\Supreme Court Panel\Replication\merged_replication_data.dta"

******Means by Month with Analytical Month-Specific Weights
***January 2013=Lowest, 2.20
***July 2016=Highest, 2.52
***May 2012
mean LEGITS6 [aw=may2012wt1]
***July 2012
mean LEGITS8 [aw=jul2012wt1]
***January 2013
mean LEGITS14 [aw=jan2013wt1]
***July 2013
mean LEGITS20 [aw=jul2013wt1]
**July 2014
mean LEGITS32 [aw=jul2014wt1]
**January 2015
mean LEGITS38 [aw=jan2015wt1]
**May 2015
mean LEGITS42 [aw=may2015wt1]
**July 2015
mean LEGITS44 [aw=jul2015wt1]
**January 2016
mean LEGITS50 [aw=jan2016wt1]
**March 2016
mean LEGITS52 [aw=mar2016wt1]
**July 2016
mean LEGITS56 [aw=jul2016wt1]

***Create initial measure
gen initial=.
replace initial=LEGITS6
replace initial=LEGITS8 if initial==.
replace initial=LEGITS14 if initial==.
replace initial=LEGITS20 if initial==.
replace initial=LEGITS32 if initial==.
replace initial=LEGITS38 if initial==.
replace initial=LEGITS42 if initial==.
replace initial=LEGITS44 if initial==.
replace initial=LEGITS50 if initial==.
replace initial=LEGITS52 if initial==.
replace initial=LEGITS56 if initial==.


***Create final measure
gen final=.
replace final=LEGITS56
replace final=LEGITS52 if final==.
replace final=LEGITS50 if final==.
replace final=LEGITS44 if final==.
replace final=LEGITS42 if final==.
replace final=LEGITS38 if final==.
replace final=LEGITS32 if final==.
replace final=LEGITS20 if final==.
replace final=LEGITS14 if final==.
replace final=LEGITS8 if final==.
replace final=LEGITS6 if final==.



***Individual wave participation indicators
gen partic1=0
replace partic1=1 if LEGITS6!=.
gen partic2=0
replace partic2=1 if LEGITS8!=.
gen partic3=0
replace partic3=1 if LEGITS14!=.
gen partic4=0
replace partic4=1 if LEGITS20!=.
gen partic5=0
replace partic5=1 if LEGITS32!=.
gen partic6=0
replace partic6=1 if LEGITS38!=.
gen partic7=0
replace partic7=1 if LEGITS42!=.
gen partic8=0
replace partic8=1 if LEGITS44!=.
gen partic9=0
replace partic9=1 if LEGITS50!=.
gen partic10=0
replace partic10=1 if LEGITS52!=.
gen partic11=0
replace partic11=1 if LEGITS56!=.

****Number of Waves Participated
gen waves_partic=.
replace waves_partic=partic1+partic2+partic3+partic4+partic5+partic6+partic7+partic8+partic9+partic10+partic11
***Initial to Final
gen init_final=final-initial
gen ab_init_final=abs(final-initial)

***Average Distance for participating in more than 3, 1.24
mean ab_init_final if waves_partic>3
***Average Distance for participating in all 11, 1.21
mean ab_init_final if waves_partic==11

***Table D1
tab ab_init_final if waves_partic>=3

***Preparing Data for Diff-in-Diff
sort wustlid

****Conservative Dichotomous indicator for anyone identifying as slightly conservative to very conservative
gen CONSERVATIVE=.
replace CONSERVATIVE=1 if IDEOL7SP>4 & IDEOL7SP<=7 
replace CONSERVATIVE=0 if IDEOL7SP<5 & IDEOL7SP>=1 

***Liberal Indicator for anyone identifying as slightly liberal to very liberal
gen LIBERAL=.
replace LIBERAL=1 if IDEOL7SP<4 & IDEOL7SP>=1 
replace LIBERAL=0 if IDEOL7SP>5 & IDEOL7SP<=7 

***Indicator for Republican Identification, includes leaners
gen REP=.
replace REP=1 if PID7_MAXN>4 & PID7_MAXN<=7 
replace REP=0 if PID7_MAXN<5 & PID7_MAXN>=1 

***Indicator for Democratic Identification, includes leaners

gen DEM=.
replace DEM=1 if PID7_MAXN<4 & PID7_MAXN>=1 
replace DEM=0 if PID7_MAXN>3 & PID7_MAXN<=7 

***Expand Data for Difference-in-Difference

expand 11
sort wustlid
***Create period indicators
egen period = fill (1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11)
sort wustlid period

***Match period indicators to diffuse support measures at a given period
gen LEGIT_T=.
replace LEGIT_T=LEGITS6 if period==1
replace LEGIT_T=LEGITS8 if period==2
replace LEGIT_T=LEGITS14 if period==3
replace LEGIT_T=LEGITS20 if period==4
replace LEGIT_T=LEGITS32 if period==5
replace LEGIT_T=LEGITS38 if period==6
replace LEGIT_T=LEGITS42 if period==7
replace LEGIT_T=LEGITS44 if period==8
replace LEGIT_T=LEGITS50 if period==9
replace LEGIT_T=LEGITS52 if period==10
replace LEGIT_T=LEGITS56 if period==11

***Match period indicators to specific support measures at a given period

gen APPR_T=.

replace APPR_T=APPRSCRTS6 if period==1
replace APPR_T=APPRSCRTS8 if period==2
replace APPR_T=APPRSCRTS14 if period==3
replace APPR_T=APPRSCRTS20 if period==4
replace APPR_T=APPRSCRTS32 if period==5
replace APPR_T=APPRSCRTS38 if period==6
replace APPR_T=APPRSCRTS42 if period==7
replace APPR_T=APPRSCRTS44 if period==8
replace APPR_T=APPRSCRTS50 if period==9
replace APPR_T=APPRSCRTS52 if period==10
replace APPR_T=APPRSCRTS56 if period==11

***Match period indicators to diffuse support measures at the lagged period

gen LEGIT_LAG=.
replace LEGIT_LAG=LEGITS6 if period==2
replace LEGIT_LAG=LEGITS8 if period==3
replace LEGIT_LAG=LEGITS14 if period==4
replace LEGIT_LAG=LEGITS20 if period==5
replace LEGIT_LAG=LEGITS32 if period==6
replace LEGIT_LAG=LEGITS38 if period==7
replace LEGIT_LAG=LEGITS42 if period==8
replace LEGIT_LAG=LEGITS44 if period==9
replace LEGIT_LAG=LEGITS50 if period==10
replace LEGIT_LAG=LEGITS52 if period==11

***Match period indicators to specific support measures at the lagged period

gen APPR_LAG=.
replace APPR_LAG=APPRSCRTS6 if period==2
replace APPR_LAG=APPRSCRTS8 if period==3
replace APPR_LAG=APPRSCRTS14 if period==4
replace APPR_LAG=APPRSCRTS20 if period==5
replace APPR_LAG=APPRSCRTS32 if period==6
replace APPR_LAG=APPRSCRTS38 if period==7
replace APPR_LAG=APPRSCRTS42 if period==8
replace APPR_LAG=APPRSCRTS44 if period==9
replace APPR_LAG=APPRSCRTS50 if period==10
replace APPR_LAG=APPRSCRTS52 if period==11

sort  wustlid period 

*** Set time periiod for fixed effects
xtset  wustlid period 

***fixed Effects model
xtreg LEGIT_T LEGIT_LAG APPR_T , fe  
 estimates store fixed
 ****Random Effects model
xtreg LEGIT_T LEGIT_LAG APPR_T , re  
 estimates store random

hausman fixed random

xtreg LEGIT_T LEGIT_LAG APPR_LAG  [pw=weights], fe  cl(wustlid)

***fixed Effects model

xtreg LEGIT_T LEGIT_LAG APPR_LAG , fe  
 estimates store fixed
  ****Random Effects model

xtreg LEGIT_T LEGIT_LAG APPR_LAG , re  
 estimates store random

hausman fixed random


***


***Diff in Diff

****Creating Dummies for each time period that limit it to wave to wave transitions.
gen time1=.
replace time1=1 if period==2
replace time1=0 if period==1
gen time2=.
replace time2=1 if period==3
replace time2=0 if period==2
gen time3=.
replace time3=1 if period==4
replace time3=0 if period==3
gen time4=.
replace time4=1 if period==5
replace time4=0 if period==4
gen time5=.
replace time5=1 if period==6
replace time5=0 if period==5
gen time6=.
replace time6=1 if period==7
replace time6=0 if period==6
gen time7=.
replace time7=1 if period==8
replace time7=0 if period==7
gen time8=.
replace time8=1 if period==9
replace time8=0 if period==8
gen time9=.
replace time9=1 if period==10
replace time9=0 if period==9
gen time10=.
replace time10=1 if period==11
replace time10=0 if period==10

***Generating the Diff-in-Diff Variables which are partisanship X time period
gen did1_REP=REP*time1
gen did2_REP=REP*time2
gen did3_REP=REP*time3
gen did4_REP=REP*time4
gen did5_REP=REP*time5
gen did6_REP=REP*time6
gen did7_REP=REP*time7
gen did8_REP=REP*time8
gen did9_REP=REP*time9
gen did10_REP=REP*time10

***Limit Sample to Republicans and Democrats
keep if REP==1 | DEM==1
***Table E1 Column 1
reg LEGIT_T did1_REP time1 REP, r
margins, at(did1_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 2
reg LEGIT_T did2_REP time2 REP, r
margins, at(did2_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 3
reg LEGIT_T did3_REP time3 REP, r
margins, at(did3_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 4
reg LEGIT_T did4_REP time4 REP, r
margins, at(did4_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 5
reg LEGIT_T did5_REP time5 REP, r
margins, at(did5_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 6
reg LEGIT_T did6_REP time6 REP, r
margins, at(did6_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 7
reg LEGIT_T did7_REP time7 REP, r
margins, at(did7_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 8
reg LEGIT_T did8_REP time8 REP, r
margins, at(did8_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 9
reg LEGIT_T did9_REP time9 REP, r
margins, at(did9_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))
***Table E1 Column 10
reg LEGIT_T did10_REP time10 REP, r
margins, at(did10_REP==(0(1)1) time==(0(1)1) REP==(0(1)1))

clear
***Alternative with Conservatives/Liberals

***Reset Data for Table E2

use "C:\Users\ptucker\Dropbox\Supreme Court Panel\Replication\merged_replication_data.dta"



***Create initial measure
gen initial=.
replace initial=LEGITS6
replace initial=LEGITS8 if initial==.
replace initial=LEGITS14 if initial==.
replace initial=LEGITS20 if initial==.
replace initial=LEGITS32 if initial==.
replace initial=LEGITS38 if initial==.
replace initial=LEGITS42 if initial==.
replace initial=LEGITS44 if initial==.
replace initial=LEGITS50 if initial==.
replace initial=LEGITS52 if initial==.
replace initial=LEGITS56 if initial==.


***Create final measure
gen final=.
replace final=LEGITS56
replace final=LEGITS52 if final==.
replace final=LEGITS50 if final==.
replace final=LEGITS44 if final==.
replace final=LEGITS42 if final==.
replace final=LEGITS38 if final==.
replace final=LEGITS32 if final==.
replace final=LEGITS20 if final==.
replace final=LEGITS14 if final==.
replace final=LEGITS8 if final==.
replace final=LEGITS6 if final==.



***Individual wave participation indicators
gen partic1=0
replace partic1=1 if LEGITS6!=.
gen partic2=0
replace partic2=1 if LEGITS8!=.
gen partic3=0
replace partic3=1 if LEGITS14!=.
gen partic4=0
replace partic4=1 if LEGITS20!=.
gen partic5=0
replace partic5=1 if LEGITS32!=.
gen partic6=0
replace partic6=1 if LEGITS38!=.
gen partic7=0
replace partic7=1 if LEGITS42!=.
gen partic8=0
replace partic8=1 if LEGITS44!=.
gen partic9=0
replace partic9=1 if LEGITS50!=.
gen partic10=0
replace partic10=1 if LEGITS52!=.
gen partic11=0
replace partic11=1 if LEGITS56!=.

****Number of Waves Participated
gen waves_partic=.
replace waves_partic=partic1+partic2+partic3+partic4+partic5+partic6+partic7+partic8+partic9+partic10+partic11
***Initial to Final
gen init_final=final-initial
gen ab_init_final=abs(final-initial)


***Preparing Data for Diff-in-Diff
sort wustlid

****Conservative Dichotomous indicator for anyone identifying as slightly conservative to very conservative
gen CONSERVATIVE=.
replace CONSERVATIVE=1 if IDEOL7SP>4 & IDEOL7SP<=7 
replace CONSERVATIVE=0 if IDEOL7SP<5 & IDEOL7SP>=1 

***Liberal Indicator for anyone identifying as slightly liberal to very liberal
gen LIBERAL=.
replace LIBERAL=1 if IDEOL7SP<4 & IDEOL7SP>=1 
replace LIBERAL=0 if IDEOL7SP>5 & IDEOL7SP<=7 

***Indicator for Republican Identification, includes leaners
gen REP=.
replace REP=1 if PID7_MAXN>4 & PID7_MAXN<=7 
replace REP=0 if PID7_MAXN<5 & PID7_MAXN>=1 

***Indicator for Democratic Identification, includes leaners

gen DEM=.
replace DEM=1 if PID7_MAXN<4 & PID7_MAXN>=1 
replace DEM=0 if PID7_MAXN>3 & PID7_MAXN<=7 

***Expand Data for Difference-in-Difference

expand 11
sort wustlid
***Create period indicators
egen period = fill (1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11)
sort wustlid period

***Match period indicators to diffuse support measures at a given period
gen LEGIT_T=.
replace LEGIT_T=LEGITS6 if period==1
replace LEGIT_T=LEGITS8 if period==2
replace LEGIT_T=LEGITS14 if period==3
replace LEGIT_T=LEGITS20 if period==4
replace LEGIT_T=LEGITS32 if period==5
replace LEGIT_T=LEGITS38 if period==6
replace LEGIT_T=LEGITS42 if period==7
replace LEGIT_T=LEGITS44 if period==8
replace LEGIT_T=LEGITS50 if period==9
replace LEGIT_T=LEGITS52 if period==10
replace LEGIT_T=LEGITS56 if period==11

***Match period indicators to specific support measures at a given period

gen APPR_T=.

replace APPR_T=APPRSCRTS6 if period==1
replace APPR_T=APPRSCRTS8 if period==2
replace APPR_T=APPRSCRTS14 if period==3
replace APPR_T=APPRSCRTS20 if period==4
replace APPR_T=APPRSCRTS32 if period==5
replace APPR_T=APPRSCRTS38 if period==6
replace APPR_T=APPRSCRTS42 if period==7
replace APPR_T=APPRSCRTS44 if period==8
replace APPR_T=APPRSCRTS50 if period==9
replace APPR_T=APPRSCRTS52 if period==10
replace APPR_T=APPRSCRTS56 if period==11

***Match period indicators to diffuse support measures at the lagged period

gen LEGIT_LAG=.
replace LEGIT_LAG=LEGITS6 if period==2
replace LEGIT_LAG=LEGITS8 if period==3
replace LEGIT_LAG=LEGITS14 if period==4
replace LEGIT_LAG=LEGITS20 if period==5
replace LEGIT_LAG=LEGITS32 if period==6
replace LEGIT_LAG=LEGITS38 if period==7
replace LEGIT_LAG=LEGITS42 if period==8
replace LEGIT_LAG=LEGITS44 if period==9
replace LEGIT_LAG=LEGITS50 if period==10
replace LEGIT_LAG=LEGITS52 if period==11

***Match period indicators to specific support measures at the lagged period

gen APPR_LAG=.
replace APPR_LAG=APPRSCRTS6 if period==2
replace APPR_LAG=APPRSCRTS8 if period==3
replace APPR_LAG=APPRSCRTS14 if period==4
replace APPR_LAG=APPRSCRTS20 if period==5
replace APPR_LAG=APPRSCRTS32 if period==6
replace APPR_LAG=APPRSCRTS38 if period==7
replace APPR_LAG=APPRSCRTS42 if period==8
replace APPR_LAG=APPRSCRTS44 if period==9
replace APPR_LAG=APPRSCRTS50 if period==10
replace APPR_LAG=APPRSCRTS52 if period==11

sort  wustlid period 

*** Set time periiod for fixed effects
xtset  wustlid period 



gen time1=.
replace time1=1 if period==2
replace time1=0 if period==1
gen time2=.
replace time2=1 if period==3
replace time2=0 if period==2
gen time3=.
replace time3=1 if period==4
replace time3=0 if period==3
gen time4=.
replace time4=1 if period==5
replace time4=0 if period==4
gen time5=.
replace time5=1 if period==6
replace time5=0 if period==5
gen time6=.
replace time6=1 if period==7
replace time6=0 if period==6
gen time7=.
replace time7=1 if period==8
replace time7=0 if period==7
gen time8=.
replace time8=1 if period==9
replace time8=0 if period==8
gen time9=.
replace time9=1 if period==10
replace time9=0 if period==9
gen time10=.
replace time10=1 if period==11
replace time10=0 if period==10

gen did1_CON=CONSERVATIVE*time1
gen did2_CON=CONSERVATIVE*time2
gen did3_CON=CONSERVATIVE*time3
gen did4_CON=CONSERVATIVE*time4
gen did5_CON=CONSERVATIVE*time5
gen did6_CON=CONSERVATIVE*time6
gen did7_CON=CONSERVATIVE*time7
gen did8_CON=CONSERVATIVE*time8
gen did9_CON=CONSERVATIVE*time9
gen did10_CON=CONSERVATIVE*time10

keep if CONSERVATIVE==1 | LIBERAL==1

***Table E2 Column 1
reg LEGIT_T did1_CON time1 CONSERVATIVE, r
margins, at(did1_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did2_CON time2 CONSERVATIVE, r
margins, at(did2_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did3_CON time3 CONSERVATIVE, r
margins, at(did3_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did4_CON time4 CONSERVATIVE, r
margins, at(did4_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did5_CON time5 CONSERVATIVE, r
margins, at(did5_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did6_CON time6 CONSERVATIVE, r
margins, at(did6_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did7_CON time7 CONSERVATIVE, r
margins, at(did7_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did8_CON time8 CONSERVATIVE, r
margins, at(did8_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did9_CON time9 CONSERVATIVE, r
margins, at(did9_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
reg LEGIT_T did10_CON time10 CONSERVATIVE, r
margins, at(did10_CON==(0(1)1) time==(0(1)1) CONSERVATIVE==(0(1)1))
